﻿using SienSchoofsProject.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebMatrix.WebData;

namespace SienSchoofsProject.Controllers
{
    public class ReviewsController : BaseController
    {
        //
        // GET: /Reviews/

        private FilmDb _db = new FilmDb();

        public ActionResult Index([Bind(Prefix = "id")] int filmId)
        {
            try
            {
                //alternatieve methode
                //Film model = (from Films in _db.Films
                //              where Films.Id == filmId
                //              select Films).FirstOrDefault();
                //return View(model);


                var film = _db.Films.Find(filmId);
                if (film != null)
                {
                    return View(film);
                }
                return HttpNotFound();
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/Logging/Log.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException:\n" + e.StackTrace);
                }
                return View();
            }
        }

        ////
        //// GET: /Reviews/Details/5

        //public ActionResult Details(int id)
        //{
        //    return View();
        //}

        //
        // GET: /Reviews/Create

        [Authorize]
        [HttpGet]
        public ActionResult Create()
        {
            try
            {
                return View();
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/Logging/Log.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException:\n" + e.StackTrace);
                }
                return View();
            }
        }

        //
        // POST: /Reviews/Create

        [Authorize]
        [HttpPost]
        public ActionResult Create(FilmReview review)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    review.ReviewerName = WebSecurity.CurrentUserName;
                    _db.Reviews.Add(review);
                    _db.SaveChanges();
                    return RedirectToAction("Index", new { id = review.FilmId });
                }
                return View(review);
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/Logging/Log.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException:\n" + e.StackTrace);
                }
                return View();
            }
        }

        //
        // GET: /Reviews/Edit/5

        [Authorize]
        [HttpGet]
        public ActionResult Edit(int id)
        {
            try
            {
                var model = _db.Reviews.Find(id);
                return View(model);
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/Logging/Log.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException:\n" + e.StackTrace);
                }
                return View();
            }
        }

        //
        // POST: /Reviews/Edit/5

        [ValidateAntiForgeryToken]
        [Authorize(Roles = "admin")]
        [HttpPost]
        public ActionResult Edit(FilmReview review)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _db.Entry(review).State = EntityState.Modified;
                    _db.SaveChanges();
                    return RedirectToAction("Index", new { id = review.FilmId });
                }
                return View(review);
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/Logging/Log.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException:\n" + e.StackTrace);
                }
                return View();
            }
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }

        //
        // GET: /Reviews/Delete/5

        [Authorize(Roles = "admin")]
        public ActionResult Delete(int id)
        {
            try
            {
                FilmReview review = _db.Reviews.Find(id);
                if (review == null)
                {
                    return HttpNotFound();
                }
                return View(review);
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/Logging/Log.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException:\n" + e.StackTrace);
                }
                return View();
            }
        }

        //
        // POST: /Reviews/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "admin")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                FilmReview review = _db.Reviews.Find(id);
                _db.Reviews.Remove(review);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/Logging/Log.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException:\n" + e.StackTrace);
                }
                return View();
            }
        }
    }
}
